﻿#include "Game.h"

CGame::CGame(void)
{
	_contra = new CContra();
	_background = new CBackground();
	_StageManager = new CStageManager();
}
int countheight1;
int countwidth1;
int MAPDATA12[104][7];

int CGame::Game_Init(HINSTANCE hInstance, HWND hWnd)
{
	HRESULT result;
	DWORD test;
	srand(time(NULL));

	if (!Init_Direct3D(hWnd, SCREEN_WIDTH, SCREEN_HEIGHT, FULLSCREEN))
	{
		MessageBox(hWnd, L"Error initializing Direct3D", L"Error", MB_OK);
		return 0;
	}

	//sound the game
	if (!_sound.Init_DirectSound(hWnd))
	{
		MessageBox(hWnd, L"Error intializing DirectSound", L"Error", MB_OK);
		return 0;
	}
	if (!_contra->ContraLoadSound(_sound))
	{
		MessageBox(hWnd, L"Error Load DirectSound", L"Error", MB_OK);
		return 0;
	}

	D3DXCreateSprite(_d3ddev, &_spriteHandler);

	// function the load image and initializing
	CSpriteManager::getInstance()->LoadSprite(_spriteHandler);
	CExplosionManager::GetInstance()->LoadResources();

	_contra->LoadResource(_d3ddev, _spriteHandler, hInstance, hWnd);
	_background->Background_Init(_d3ddev, _spriteHandler, hInstance, hWnd);
	
	_StageManager->SetStage(1);
	start = GetTickCount();

	return 1;
}

void CGame::Game_Run(HWND hWnd)
{
	if (_d3ddev == NULL)
		return;

	if (GetTickCount() - start > 15)
	{
		start = GetTickCount();
		_contra->Contra_Run();
	}

	_contra->HandleCollisionOnMap(_StageManager->getMapGround());
	//_contra->TestSuperTurret(_StageManager->getSuperTurret());

	if (_d3ddev->BeginScene())
	{
		_d3ddev->Clear(1, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1, NULL);

		_background->Background_Render(_contra->GetPosition());
		
		_StageManager->Render(_contra->GetPosition());
		CExplosionManager::GetInstance()->Render();

		_contra->Render();
		_d3ddev->EndScene();
	}

	_d3ddev->Present(NULL, NULL, NULL, NULL);

	if (KEY_DOWN(VK_ESCAPE))
		PostMessage(hWnd, WM_DESTROY, 0, 0);
}

void CGame::Game_End(HWND hWnd)
{
	if (_d3ddev != NULL)
		_d3ddev->Release();

	//release the Direct3D object
	if (_d3d != NULL)
		_d3d->Release();

	if (_backbuffer != NULL)
		_backbuffer->Release();

	_contra->Contra_End();
	_background->Background_End();
}

CGame::~CGame(void)
{

}